package com.mach.platform.controller;

import com.mach.platform.domain.Response;
import com.mach.platform.domain.base.Menu;
import com.mach.platform.service.MenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Anonymous
 * @Description:
 * @Package: com.mach.platform.com.mach.platform.controller
 * @time 2018/04/12 17:40
 */
@RestController
@RequestMapping("$menu")
public class MenuCtrl extends BaseCtrl<Menu> {

	@Autowired
	private MenuServiceImpl menuService;

	/**
	 * -- @GetMapping(value = "/xxx/{id}")
	 * -- @PathVariable(value = "id", required = false) Long id
	 * 单个数据 的接口
	 *
	 * @param id 数据的id
	 * @return
	 */
	@Override
	public Response info (Long id) {
		return null;
	}

	/**
	 * 获取列表数据
	 * --@GetMapping(value = "/xxxs")
	 * -- @PageableDefault(size = 25) Pageable pageable
	 *
	 * @param pageable
	 * @param params
	 * @return
	 */
	@GetMapping(value = "/list")
	@Override
	public Response list (@PageableDefault(size = 25) Pageable pageable, @RequestParam Map params) {
		Page<Menu> dbResult = menuService.list(pageable, params);
		return Response.ok(dbResult);
	}

	/**
	 * -- @DeleteMapping("/xxx")
	 * -- @RequestBody Long[] ids
	 * 删除数据
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/del")
	@Override
	public Response delete (@RequestBody Long[] ids) {

		if (Arrays.asList(ids).contains(1L)){
			return Response.error("不能删除根菜单");
		}
		int effectedNums = menuService.delete(ids);
		return Response.ok(effectedNums);
	}

	/**
	 * 保存一个 对象
	 * --@PostMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param menu
	 * @return
	 */
	@PostMapping("/save")
	@Override
	public Response save (@RequestBody Menu menu) {
		Menu dbResult = menuService.save(menu);
		return Response.ok(dbResult);
	}

	/**
	 * 更新一个对象
	 * --@PutMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param menu
	 * @return
	 */
	@PutMapping("/upd")
	@Override
	public Response update (@RequestBody Menu menu) {
		Menu dbResult = menuService.update(menu);
		return Response.ok(dbResult);
	}

	/**
	 * 获取 菜单的角色
	 * @return
	 */
	@PostMapping("auth_menus")
	public Response loadMenusRoles ( ) {

		List<Menu> mapResult = menuService.loadMenusRoles();

		return Response.ok(mapResult);
	}

	/**
	 * 没有权限限制的菜单数据
	 * @return
	 */
	@PostMapping("tree")
	public Response loadMenusTreeNonAuth(){
		List<Menu> mapResult = menuService.loadMenusTreeNonAuth();
		return Response.ok(mapResult);
	}

	/**
	 * 根据角色id 获取 菜单数据
	 * @return
	 */
	@PostMapping("rmenus")
	public Response loadMenusByRoleId(@RequestParam("id") Long id){
		List<Map> mapResult = menuService.loadMenusByRoleId(id);
		return Response.ok(mapResult);
	}

	/**
	 *  获取扥请求用户对应的菜单数据
	 * @return
	 */
	@PostMapping("auth_menus_list")
	public Response loadUserAuthMenus ( ) {

		List<Map> mapResult = menuService.loadUserAuthMenus();

		return Response.ok(mapResult);
	}

	/**
	 *  获取扥请求用户对应的 权限下的功能 菜单数据
	 * @return
	 */
	@PostMapping("auth_menus_fun")
	public Response loadUserAuthMenusByFunctions (@RequestParam("url") String url ) {

		List<Map> mapResult = menuService.loadUserAuthMenusAndMenus(url);

		return Response.ok(mapResult);
	}
}
